package com.steven.im.webSocket;


import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketFactory;
import com.steven.im.webSocket.WSBaseEntity.WSBase;

import java.io.IOException;


public class WebSocketClient {
     private static WebSocket ws;


    public static WebSocket getInstance() {
        if (ws == null) {
            synchronized (WebSocket.class) {
                if (ws == null) {
                    try {
//                        String token=SPUtils.get(App.getContext(),"token","")+"";
//                        Integer userId=Integer.parseInt(SPUtils.get(App.getContext(),"userId","")+"");
                         ws = new WebSocketFactory().createSocket(WSBase.URI, WSBase.CONNECT_TIMEOUT) //ws地址，和设置超时时间
                                 .setFrameQueueSize(WSBase.FRAME_QUEUE_SIZE)//设置帧队列最大值为5
                                 .setMissingCloseFrameAllowed(false)//设置不允许服务端关闭连接却未发送关闭帧
                                 .addListener(new WebSocketListener());//添加回调监听
//                         .addHeader("userId", userId)
//                                .addHeader("token",token);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
        return ws;

    }
}
